//
//  GrideView.swift
//  SBNewProject
//
//  Created by hlt on 2023/10/11.
//

import Foundation
import UIKit

struct GrideView{
    
    /// 创建一个等宽度的View布局
    /// - Parameters:
    ///   - count: 总item个数
    ///   - height: item高度
    ///   - coloums: 一行的最大列数
    ///   - spacing: item之间的间距
    ///   - arrangedSubviewBlock  item的view
    /// - Returns: UIStackView
    static  func createFillEquallyItem(_ count:Int,height:CGFloat,coloums:Int,spacing:CGFloat,arrangedSubviewBlock:(UIView) -> Void ) -> UIStackView{
        let contentStacKview = UIStackView();
        contentStacKview.backgroundColor = .white
        contentStacKview.translatesAutoresizingMaskIntoConstraints = false;
        contentStacKview.spacing = 0;
        contentStacKview.axis = .vertical;
        let rows = (count - 1) / coloums  + 1;
        
        for _ in 0..<rows{
            let stackView = UIStackView()
            contentStacKview.addArrangedSubview(stackView)
            stackView.distribution = .fillEqually
            stackView.spacing = spacing;
            stackView.backgroundColor = .white
            for _ in 0..<coloums{
                let view = UIView()
                view.translatesAutoresizingMaskIntoConstraints = false
                stackView.addArrangedSubview(view)
                view.heightAnchor.constraint(equalToConstant: height).isActive = true
                arrangedSubviewBlock(view)
            }
        }
        return contentStacKview;
    }
    
    /// 创建一个等间距的View布局
    /// - Parameters:
    ///   - count: 总item个数
    ///   - height: item高度
    ///   - coloums: 一行的最大列数
    ///   - width: item的宽度
    ///   - arrangedSubviewBlock  item的view
    /// - Returns: UIStackView
    static  func createSpacingEquallyItem(_ count:Int,height:CGFloat,coloums:Int,width:CGFloat,arrangedSubviewBlock:(UIView) -> Void ) -> UIStackView{
        let contentStacKview = UIStackView();
        contentStacKview.backgroundColor = .white
        contentStacKview.translatesAutoresizingMaskIntoConstraints = false;
        contentStacKview.spacing = 0;
        contentStacKview.axis = .vertical;
        let rows = (count - 1) / coloums  + 1;
        
        for _ in 0..<rows{
            let stackView = UIStackView()
            contentStacKview.addArrangedSubview(stackView)
            stackView.distribution = .equalSpacing
            stackView.backgroundColor = .white
            for _ in 0..<coloums{
                let view = UIView()
                view.translatesAutoresizingMaskIntoConstraints = false
                stackView.addArrangedSubview(view)
                view.heightAnchor.constraint(equalToConstant: height).isActive = true
                view.widthAnchor.constraint(equalToConstant: width).isActive = true
                arrangedSubviewBlock(view)
            }
        }
        return contentStacKview;
    }
}
